<template>
	<view>
		<view class="header">
			<image src="https://ba.fuliaoxx.com/img/bj.jpg" mode=""></image>
			<view class="">
				<view class="">
					Hello,
				</view>
				<view class="">
					欢迎登录八戒快修
				</view>
			</view>
		</view>
		<button open-type="getPhoneNumber" @getphonenumber="getUserInfo">
			<view class="btn">
				手机号快捷登录
			</view>
		</button>
		<view class="" style="text-align: center;color: #bababa;margin-top: 14rpx;font-size: 30rpx;" @click="close">
			暂不登陆
		</view>
	</view>
</template>
<script>
	import {
		login,
		toWxLog
	} from "@/request/api/login.js"
	export default {
		data() {
			return {
				code: null
			}
		},
		methods: {
			close() {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			},
			getUserInfo(e) {
				uni.showLoading()
				let that = this
				wx.login({
					success(res) {
						if (res.code) {
							that.code = res.code
							that.getPhoneNumber(e)
						} else {}
					},
					complete(err) {
						console.log(err)
					}
				})
			},
			// 手机号授权
			getPhoneNumber(e) {
				let that = this;
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					// 如果拒绝授权登录，就走这里
					uni.hideLoading()
				} else {
					// 同意手机号授权登录
					login(that.code).then(res => {
						let param = {
							session_key: res.data.session_key,
							openid: res.data.openid,
							iv: e.detail.iv,
							encryptedData: e.detail.encryptedData,
						}
						let salt = uni.setStorageSync('salt')
						if (salt) {
							param['salt'] = salt;
							uni.removeStorageSync('salt')
						}
						toWxLog(param).then(res1 => {
							uni.hideLoading()
							uni.setStorageSync("userInfo", res1.data)
							uni.setStorageSync("token", res1.data.token)
							uni.reLaunch({
								url: "/pages/index/index"
							})
						})
					})


					// 拿到手机号的授权code和上面微信授权的code之后。就可以传给后端，登陆啦
				}
			},
		}
	}
</script>
<style>
	page {
		background-color: white;
	}
</style>
<style scoped lang="scss">
	.header {
		display: flex;
		align-items: center;
		font-size: 50rpx;
		font-weight: 600;
		margin: 100rpx 40rpx;
		color: #343434;

		image {
			width: 80rpx;
			height: 110rpx;
			margin-right: 30rpx;
		}
	}

	button {
		background-color: rgba(1, 1, 1, 0) !important;
		border: none !important;
	}

	button::before {
		border: none !important;
	}

	button::after {
		border: none !important;
	}

	.btn {
		color: white;
		background-color: rgb(7, 193, 96);
		margin: 30rpx;
		text-align: center;
		border-radius: 100px;
		font-size: 32rpx;
		padding: 12rpx;
	}
</style>